package com.zkingsoft.redis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.matrix.core.exception.GlobleException;
import com.zkingsoft.common.constance.AppMessageCode;

/**
 * redis登录拦截器，判断用户是已经登录且登录信息没有失效
 * 
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date 2017年12月8日
 */
public class RedisLoginInterceptor implements HandlerInterceptor {

	@Autowired
	RedisUserLoginUtils redisUserLoginUtils;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		
		if (redisUserLoginUtils.isUserLogin()) {
			return true;
		} else {
			// 没有登录跳转授权界面
			throw new GlobleException(AppMessageCode.User.LOGIN_TIMEOUT);
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	}
}
